#include <msgthread.h>
MsgThread::MsgThread(SOCKET receive, SOCKET send_s, ChatDbHandler *db){
    receive_sock = receive;
    send_sock = send_s;
    chat_db = db;
}

void MsgThread::run(){
    TransMsg trans_msg = {};
    while (true){
        cout << "Recv ..." << endl;
        if (SockRecvMsg((char*)&trans_msg, receive_sock) <= 0){
            break;
        }else{
            //客户端有数据送达
            DealMsg(trans_msg);
            memset(&trans_msg, 0, sizeof(TransMsg));
        }
    }
    SockClose(receive_sock);
    SockClose(send_sock);
    return;
}

void MsgThread::DealMsg(TransMsg trans_msg){
    MsgType tp = trans_msg.msg_type;
    ShowTranslateMessage(trans_msg);
    switch (tp){
        case REQ_LOGIN  : DealReqLogin(trans_msg);break;
        case REQ_REGIST : DealReqRegist(trans_msg);break;
        case MESSAGE    : DealMessage(trans_msg);break;
        case REQ_HEARTB : DealReqHeartB(trans_msg);break;
        default         : cout << "i can't deal this type message" << endl;break;
    }
}

//将void*改为自己想要的类型，void *是个指针;
int GetdbpasswordCb(void *data, int col, char **array, char **col_name){
    string *str = (string *)data;
    *str = array[0];
    return 0;
}

void MsgThread::DealReqLogin(const TransMsg &msg){
//拆分消息获得name，password，同时查询数据库中是否存在这个name
    string name = msg.msg_from;

    //CONTENT:[PASSWORD],这里得到的是[PASSWORD],就直接存入数据库里,所以在数据库中[password]的才是通过客户端添加数据的;
    string password = msg.msg_content;
    string dbpassword = "";
    string item = "PASSWORD";
    string condition = "NAME = '" + name + "'";
    int rs = chat_db->GetPwInUserByName(dbpassword, item, condition, GetdbpasswordCb);
    cout << "dbpassword: " + dbpassword << endl;
    //登录成功，登录失败密码错误，登录失败用户不存在
    TransMsg trans_msg;
    if (rs == SQLITE_OK) {
        if (password == dbpassword){
            CreateRspLoginMsg(trans_msg, name.c_str(), "success");
            send(send_sock, (char*)&trans_msg, sizeof(TransMsg), 0);
//            SockSendMsg((char*)&trans_msg, send_sock);
            socket_user_name = name;
        }
        else{
            CreateRspLoginMsg(trans_msg, name.c_str(), "fail,password");
            send(send_sock, (char*)&trans_msg, sizeof(TransMsg), 0);
//            SockSendMsg((char*)&trans_msg, send_sock);
        }
    }
    else {
        CreateRspLoginMsg(trans_msg, name.c_str(), "fail,nonexist");
        send(send_sock, (char*)&trans_msg, sizeof(TransMsg), 0);
//        SockSendMsg((char*)&trans_msg, send_sock);
    }
}




void MsgThread::DealReqRegist(const TransMsg &msg){
    string name = msg.msg_from;
    string password = msg.msg_content;

    //如果在USER数据库中存在,发回注册失败，如果不在，将数据添加到USER中，然后发回注册成功;
    int rs = chat_db->InsertUserItem(name, password);
    TransMsg trans_msg;
    if (rs != SQLITE_OK){
        cout << "regist fail, sendMsg" << endl;
        CreateRspRegistMsg(trans_msg, name.c_str(), "fail");
        SockSendMsg((char *)&trans_msg, send_sock);
    }else {
        emit RegistComing(msg.msg_from);
        cout << "regist success, sendMsg" << endl;
        CreateRspRegistMsg(trans_msg, name.c_str(), "success");
        SockSendMsg((char *)&trans_msg, send_sock);
    }
}


void MsgThread::DealMessage(TransMsg &msg){
    //转发给另外的socket
    emit MessageComing(msg);
}

void MsgThread::DealReqHeartB(const TransMsg &msg){
    //更新时间;
    emit HeartBComing(msg);
}


void MsgThread::IsMyMsg(const TransMsg &msg){
    //ShowTranslateMessage(msg);
    if(msg.msg_to == socket_user_name){
        //cout << "send msg";
        SockSendMsg((char *)&msg, send_sock);
    }
}

//这个不在ReqHeartB之后立即发送，由定时器启动，每隔一段时间发送;
void MsgThread::SendRspHeartB(string content){
    TransMsg trans_msg;
    if (socket_user_name != ""){
        CreateRspHeartbMsg(trans_msg, socket_user_name.c_str(), content.c_str());
        ShowTranslateMessage(trans_msg);
        SockSendMsg((char *)&trans_msg, send_sock);
    }

}

